﻿using PhilipService;
using PhilipSupport.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PhilipSupport
{
    public partial class LoginForm : Form
    {
        private const int CS_DropSHADOW = 0x20000;
        private const int GCL_STYLE = (-26);
        readonly PrivateFontCollection pfc = new PrivateFontCollection();

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);

        public LoginForm()
        {
            InitializeComponent();
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
            panel.MouseDown += LoginForm_MouseDown;
            panel.MouseMove += LoginForm_MouseMove;
        }

        private void LoginForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - downPoint.X,
                    this.Location.Y + e.Y - downPoint.Y);
            }
        }

        Point downPoint;

        private void LoginForm_MouseDown(object sender, MouseEventArgs e)
        {
            downPoint = new Point(e.X, e.Y);
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            input_username.Text = "";
            input_pwd.Text = "";
            Task.Factory.StartNew(() =>
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\\AliHYAiHei.ttf";
                
                pfc.AddFontFile(path);
                PhilipContext.FontCollection = pfc;
                Font f = new Font(pfc.Families[0], 16);
                PhilipContext.CustomFont = f;
            });
        }

        private void btn_viewPwd_Click(object sender, EventArgs e)
        {
            if (!input_pwd.PwdVisible) {
                input_pwd.PasswordChar = '\0';
                input_pwd.PwdVisible = true;
            }
            else
            {
                input_pwd.PasswordChar = '*';
                input_pwd.PwdVisible = false;
            }
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(input_username.Text)) {
                MessageBox.Show("用户名不能为空");
                return;
            }

            if (string.IsNullOrEmpty(input_pwd.Text))
            {
                MessageBox.Show("密码不能为空");
                return;
            }

            var username = input_username.Text;
            var password = input_pwd.Text;

            Task.Factory.StartNew(new Action(() =>
            {
                var manager = new RestServiceManager();
                var response = manager.Login(username, password);
                if (response.Code == 0)
                {
                    PhilipContext.UserId = response.CustomerId;
                    PhilipContext.UserName = response.Name;
                    PhilipContext.Phone = response.Phone;
                    PhilipContext.OwnId = response.OwnId;
                    PhilipContext.HostId = response.HostId;
                    PhilipContext.LoginAccount = input_username.Text;
                    this.Invoke(new Action(() =>
                    {
                        var form = new SupportMain();
                        form.Show();
                        this.Hide();
                    }));
                }
                else
                {
                    this.Invoke(new Action(() =>
                    {
                        MessageBox.Show("登录失败，请检查用户名密码是否正确", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }));
                }
            }));            
        }

        private void LoginForm_Paint(object sender, PaintEventArgs e)
        {
            var pen = new Pen(Color.LightGray);
            e.Graphics.DrawLine(pen, new Point(0, 0), new Point(this.Width, 0));
            e.Graphics.DrawLine(pen, new Point(this.Width-1, 0), new Point(this.Width-1, this.Height));
            e.Graphics.DrawLine(pen, new Point(0, this.Height-1), new Point(this.Width-1, this.Height-1));
            e.Graphics.DrawLine(pen, new Point(0, this.Height-1), new Point(0, 0));
        }


        private void btn_min_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void input_pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                btn_login_Click(null, null);
            }
        }
    }
}
